<?
	/* ****************** LOAD HAM **************** */
	
	function LaySoLuongSanPham($id){
		global $db, $prefix;
		$count = 0;
		$rs = $db->query("SELECT * FROM ".$prefix."sanpham WHERE t_loaisanpham = ".$id." AND t_hienthi = 1");
		$count = $db->num_rows($rs);
		return $count;
	}
	
	function LaySoLuongTinRaoVat($id){
		global $db, $prefix;
		$count = 0;
		$rs = $db->query("SELECT * FROM ".$prefix."raovat WHERE rv_loairaovat = ".$id." AND rv_hienthi = 1");
		$count = $db->num_rows($rs);
		return $count;
	}
	function LaySoLuongTinTuyenDung($id){
		global $db, $prefix;
		$count = 0;
		$rs = $db->query("SELECT * FROM ".$prefix."tuyendung WHERE td_loaituyendung = ".$id." AND td_hienthi = 1");
		$count = $db->num_rows($rs);
		return $count;
	}
	
	function layDanhSachSubCatId($id){
		global $db, $prefix;
		$list = "";
		$rs = $db->query("SELECT * FROM ".$prefix."loaisanpham WHERE c_id_cha = ".$id." AND c_hienthi = 1 order by c_thutu ASC");
		$i = 0;
		
		while($row = $db->fetch_array($rs)){
			$i++;
			if($i == $db->num_rows($rs)){
				$list .= $row[c_id];
			}else{
				$list .= $row[c_id] . ",";
			}
		}
		return $list;
	}
	
	function layQuocGia($id){
		global $db, $prefix;
		$ten = "";
		$rs = $db->query("SELECT * FROM ".$prefix."quocgia WHERE qg_id = ".$id."");
		$row = $db->fetch_array($rs);
		$ten = stripslashes($row[qg_ten]);
		
		return $ten;
	}
	
	function layTinhThanh($id){
		global $db, $prefix;
		$ten = "";
		$rs = $db->query("SELECT * FROM ".$prefix."tinhthanh WHERE tt_id = ".$id."");
		$row = $db->fetch_array($rs);
		$ten = stripslashes($row[tt_ten]);
		
		return $ten;
	}
	
	
	function tenNguoiDung($id){
		global $db, $prefix;
		$u_name = "";
		$rs = $db->query("SELECT * FROM ".$prefix."nguoidung WHERE u_id = ".$id."");
		$row = $db->fetch_array($rs);
		if($row[u_banned] == 0){
			$u_name = "<strike>" . stripslashes($row[u_name]) . "</strike>";
		}else{
			$u_name = stripslashes($row[u_name]);
		}
		
		return $u_name;
	}
	
	function tenNguoiDung2($id){
		global $db, $prefix;
		$u_name = "";
		$rs = $db->query("SELECT * FROM ".$prefix."nguoidung WHERE u_id = ".$id."");
		$row = $db->fetch_array($rs);
		$u_name = stripslashes($row[u_name]);
		return $u_name;
	}
	
	function banned($id,$str){
		global $db, $prefix;
		$u_name = "";
		$rs = $db->query("SELECT * FROM ".$prefix."nguoidung WHERE u_id = ".$id."");
		$row = $db->fetch_array($rs);
		if($row[u_banned] == 0){
			$u_name = "<strike>" . $str . "</strike>";
		}else{
			$u_name = $str;
		}
		
		return $u_name;
	}
	
	
	function loadLoaiTien($id){
		global $arrLoaiTien, $db, $prefix;
		$tien = "";
		for($i=0; $i<count($arrLoaiTien); $i++){
			if($i == $id){
				$tien = $arrLoaiTien[$i];
			}
		}
		return $tien;
	}
	
	function loadSoTinNhan($id){
		global $db, $prefix;
		$count = 0;
		$sql = "SELECT * FROM ".$prefix."tinnhanrieng where tnr_nguoinhan = ".$id." AND tnr_tinhtrang = 0";
		$rs = $db->query($sql);
		$count = $db->num_rows($rs);
		
		return $count;
	}
	
	function loadSoTinNhan2($id){
		global $db, $prefix;
		$count = 0;
		$rs = $db->query("SELECT * FROM ".$prefix."tinnhan where tn_nguoinhan = ".$id."");
		$count = $db->num_rows($rs);
		
		return $count;
	}
	
	function loadSoSanPhamLuu($id){
		global $db, $prefix;
		$count = 0;
		$rs = $db->query("SELECT u_sanphamluu FROM ".$prefix."nguoidung where u_id = ".$id." AND u_banned = 1");
		$row = $db->fetch_array($rs);
		if($row[u_sanphamluu] != ""){
			$arrList = split(";",$row[u_sanphamluu]);
			$count = count($arrList);
		}
		
		return $count;
	}
	
	function loadSoComment($id){
		global $db, $prefix;
		$count = 0;
		$rs = $db->query("SELECT * FROM ".$prefix."sanpham_comment where cm_sanpham_id = '".$id."'");
		$count = $db->num_rows($rs);
		
		return $count;
	}
	
	function loadSoTinTucComment($id){
		global $db, $prefix;
		$count = 0;
		$rs = $db->query("SELECT * FROM ".$prefix."tintuc_comment where cm_tintuc_id = '".$id."'");
		$count = $db->num_rows($rs);
		
		return $count;
	}
	
	function loadPhuongThucBanHang($id){
		global $arrPhuongThucBanHang;
		$str = "";
		for($i=0;$i<count($arrPhuongThucBanHang);$i++){
			if($i == $id){
				$str = $arrPhuongThucBanHang[$i];
			}
		}
		return $str;
	}
	
	function updateSoLanXem($pid){
		global $db,$prefix;
		$viewed = 0;
		$rs = $db->query("SELECT * FROM ".$prefix."sanpham WHERE t_id = '".$pid."' AND t_hienthi = 1");
		if($db->num_rows($rs) != 0){
			$row = $db->fetch_array($rs);
			$viewed = $row[t_solanxem] + 1;
		}
		
		$db->query("UPDATE ".$prefix."sanpham SET t_solanxem = '".$viewed."' WHERE t_id = '".$pid."' AND t_hienthi = 1");
	}
	
	function updateXemGanNhat($pid){
		global $db,$prefix;
		$db->query("UPDATE ".$prefix."sanpham SET t_xemgannhat = now() WHERE t_id = '".$pid."' AND t_hienthi = 1");
	}
	
	
	function loadNickChat($id,$text){
		$arrList = split(";",$text);
		$str = "";
		for($i=0;$i<count($arrList);$i++){
			$tmp = split("-",$arrList[$i]);
			if($i == $id){
				$str = $tmp[1];
			}
		}
		return $str;
	}
	
	function loadAvatar($id){
		global $db, $prefix;
		$u_avatar = "";
		$rs = $db->query("SELECT u_avatar FROM ".$prefix."nguoidung WHERE u_id = '".$id."' AND u_banned = 1");
		$row = $db->fetch_array($rs);
			$u_avatar = stripslashes($row[u_avatar]);
		
		return $u_avatar;
	}
	
	function loadChuKy($id){
		global $db, $prefix,$myString;
		$u_chuky = "";
		$rs = $db->query("SELECT * FROM ".$prefix."nguoidung WHERE u_id = '".$id."' AND u_banned = 1");
		$row = $db->fetch_array($rs);
		$u_chuky = $myString->unQuote(stripslashes($row[u_chuky]));
		
		return $u_chuky;
	}
	
	function demtinnhan($uid,$type){
		global $db, $prefix;
		$count = 0;
		if($type == 0){
			$sql = "SELECT * FROM ".$prefix."tinnhanrieng WHERE tnr_nguoinhan = '".$uid."'";
		}else if($type == 1){
			$sql = "SELECT * FROM ".$prefix."tinnhanrieng WHERE tnr_nguoigui = '".$uid."'";
		}else if($type == 2){
			$sql = "SELECT * FROM ".$prefix."tinnhanrieng WHERE tnr_nguoinhan = '".$uid."' AND tnr_tinhtrang = 0";
		}else if($type == 3){
			$sql = "SELECT * FROM ".$prefix."tinnhanrieng WHERE tnr_nguoinhan = '".$uid."' AND tnr_danhdau = 1";
		}else{
			$sql = "SELECT * FROM ".$prefix."tinnhanrieng";
		}
		$rs = $db->query($sql);
		
		$count = $db->num_rows($rs);
		
		return $count;
	}
	
	function tenChungLoaiTinTuc($id){
		global $db, $prefix;
		$ten = "";
		$rs = $db->query("SELECT * FROM ".$prefix."loaitintuc WHERE ltt_id = '".$id."' AND ltt_hienthi = 1");
		$row = $db->fetch_array($rs);
		$ten = stripslashes($row[ltt_ten]);
		
		return $ten;
	}
	
	function tenChungLoaiRaoVat($id){
		global $db, $prefix;
		$ten = "";
		$rs = $db->query("SELECT * FROM ".$prefix."loairaovat WHERE rvc_id = '".$id."' AND rvc_hienthi = 1");
		$row = $db->fetch_array($rs);
		$ten = stripslashes($row[rvc_ten]);
		
		return $ten;
	}
	
	function tenChungLoaiTuyenDung($id){
		global $db, $prefix;
		$ten = "";
		$rs = $db->query("SELECT * FROM ".$prefix."loaituyendung WHERE ltd_id = '".$id."' AND ltd_hienthi = 1");
		$row = $db->fetch_array($rs);
		$ten = stripslashes($row[ltd_ten]);
		
		return $ten;
	}
	
	function loadConfigUser($id,$type){
		global $db, $prefix;
		$cf = 0;
		$rs = $db->query("SELECT * FROM ".$prefix."nguoidung WHERE u_id = '".$id."' AND u_banned = 1");
		$row = $db->fetch_array($rs);
		if($type == 0){
			$cf = $row[u_cfg_comment];
		}else{
			$cf = $row[u_cfg_tinnhan];
		}
		
		return $cf;
	}
	
	
	function RaoVat_updateSoLanXem($rid){
		global $db,$prefix;
		$viewed = 0;
		$rs = $db->query("SELECT * FROM ".$prefix."raovat WHERE rv_id = '".$rid."' AND rv_hienthi = 1");
		if($db->num_rows($rs) != 0){
			$row = $db->fetch_array($rs);
			$viewed = $row[rv_solanxem] + 1;
		}
		
		$db->query("UPDATE ".$prefix."raovat SET rv_solanxem = '".$viewed."' WHERE rv_id='".$rid."' AND rv_hienthi=1");
	}
	
	function RaoVat_updateXemGanNhat($rid){
		global $db,$prefix;
		$db->query("UPDATE ".$prefix."raovat SET rv_xemgannhat = now() WHERE rv_id = '".$rid."' AND rv_hienthi = 1");
	}
	
	
	function TuyenDung_updateSoLanXem($rid){
		global $db,$prefix;
		$viewed = 0;
		$rs = $db->query("SELECT * FROM ".$prefix."tuyendung WHERE td_id = '".$rid."' AND td_hienthi = 1");
		if($db->num_rows($rs) != 0){
			$row = $db->fetch_array($rs);
			$viewed = $row[td_solanxem] + 1;
		}
		
		$db->query("UPDATE ".$prefix."tuyendung SET td_solanxem = '".$viewed."' WHERE td_id='".$rid."' AND td_hienthi=1");
	}
	
	function TuyenDung_updateXemGanNhat($rid){
		global $db,$prefix;
		$db->query("UPDATE ".$prefix."tuyendung SET td_xemgannhat = now() WHERE td_id = '".$rid."' AND td_hienthi = 1");
	}
	
	
	function loadSoSanPhamTheoUser($id){
		global $db, $prefix;
		$count = 0;
		$rs = $db->query("SELECT * FROM ".$prefix."sanpham where t_nguoidang = '".$id."' AND t_hienthi = 1");
		$count = $db->num_rows($rs);
		
		return $count;
	}
	
	function getloaisanpham($id){
		global $db, $prefix;
		$cat_name = "";
		$rs = $db->query("SELECT * FROM ".$prefix."loaisanpham where c_id = '".$id."' AND c_hienthi = 1");
		if($db->num_rows($rs) != 0){
			$row = $db->fetch_array($rs);
			$cat_name = $row[c_ten];
		}else{
			$cat_name = "Thư mục cha";
		}
		
		return $cat_name;
	}
	
	function ImgActive($hienthi){
		if($hienthi == 0){
			echo "<img src=\"images/inactived.gif\" border=\"0\">";
		}else{
			echo "<img src=\"images/actived.gif\" border=\"0\">";
		}
	}
	
	function getSanPham($id){
		global $db, $prefix;
		$t_tieude = "";
		$rs = $db->query("SELECT * FROM ".$prefix."sanpham where t_id = '".$id."' AND t_hienthi = 1");
		if($db->num_rows($rs) != 0){
			$row = $db->fetch_array($rs);
			$t_tieude = $row[t_tieude];
		}
		
		return $t_tieude;
	}
	
	
	function getLoaiRaoVat($id){
		global $db, $prefix;
		$cat_name = "";
		$rs = $db->query("SELECT * FROM ".$prefix."loairaovat where rvc_id = '".$id."' AND rvc_hienthi = 1");
		if($db->num_rows($rs) != 0){
			$row = $db->fetch_array($rs);
			$cat_name = $row[rvc_ten];
		}else{
			$cat_name = "Thư mục cha";
		}
		
		return $cat_name;
	}
	
	function getloaituyendung($id){
		global $db, $prefix;
		$cat_name = "";
		$rs = $db->query("SELECT * FROM ".$prefix."loaituyendung where ltd_id = '".$id."' AND ltd_hienthi = 1");
		if($db->num_rows($rs) != 0){
			$row = $db->fetch_array($rs);
			$cat_name = $row[ltd_ten];
		}else{
			$cat_name = "Thư mục cha";
		}
		
		return $cat_name;
	}
	
	function getTinTuc($id){
		global $db, $prefix;
		$tt_tieude = "";
		$rs = $db->query("SELECT * FROM ".$prefix."tintuc where tt_id = '".$id."' AND tt_hienthi = 1");
		if($db->num_rows($rs) != 0){
			$row = $db->fetch_array($rs);
			$tt_tieude = $row[tt_tieude];
		}
		
		return $tt_tieude;
	}
	
	function loadLoaiGopY($id){
		global $arrGopY;
		$str = "";
		for($i=0;$i<count($arrGopY); $i++){
			if($i == $id){
				$str = $arrGopY[$i];
			}
		}
		
		return $str;
	}
	
	function loadTenLoaiSanPham($id){
		global $db, $prefix;
		$cat_name = "";
		$rs = $db->query("SELECT * FROM ".$prefix."loaisanpham where c_id = '".$id."' AND c_hienthi = 1");
		if($db->num_rows($rs) != 0){
			$row = $db->fetch_array($rs);
			$cat_name = $row[c_ten];
		}
		
		return $cat_name;
	}
	
	function loadTenChungLoaiTheoSanPham($id){
		global $db, $prefix;
		$c_ten = "";
		$rs = $db->query("SELECT c.c_ten FROM ".$prefix."sanpham as s, ".$prefix."loaisanpham as c WHERE c.c_id = s.t_loaisanpham AND t_id = '".$id."'");
		if($db->num_rows($rs) != 0){
			$row = $db->fetch_array($rs);
			$c_ten = stripslashes($row[c_ten]);
		}
		return $c_ten;
	}
	
	function loadRaoVat($id){
		global $db, $prefix;
		$rv_tieude = "";
		$rs = $db->query("SELECT * FROM ".$prefix."raovat WHERE rv_id = '".$id."' AND rv_hienthi = 1");
		if($db->num_rows($rs) != 0){
			$row = $db->fetch_array($rs);
			$rv_tieude = stripslashes($row[rv_tieude]);
		}
		return $rv_tieude;
	}
	
	function loadTuyenDung($id){
		global $db, $prefix;
		$td_tieude = "";
		$rs = $db->query("SELECT * FROM ".$prefix."tuyendung WHERE td_id = '".$id."' AND td_hienthi = 1");
		if($db->num_rows($rs) != 0){
			$row = $db->fetch_array($rs);
			$rv_tieude = stripslashes($row[td_tieude]);
		}
		return $td_tieude;
	}
	
	function loadSanPham($id){
		global $db, $prefix;
		$t_tieude = "";
		$rs = $db->query("SELECT * FROM ".$prefix."sanpham WHERE t_id = '".$id."' AND t_hienthi = 1");
		if($db->num_rows($rs) != 0){
			$row = $db->fetch_array($rs);
			$rv_tieude = stripslashes($row[t_tieude]);
		}
		return $t_tieude;
	}
	
	
	function loadTopLoaiSanPham(){
		global $db, $prefix;
		$id = 0;
		$rs = $db->query("SELECT * FROM ".$prefix."loaisanpham where c_hienthi = 1 AND c_id_cha != 0 ORDER BY c_id DESC LIMIT 1");
		if($db->num_rows($rs) != 0){
			$row = $db->fetch_array($rs);
			$id = $row[c_id];
		}
		return $id;
	}
	
	function loadTopLoaiRaoVat(){
		global $db, $prefix;
		$id = 0;
		$rs = $db->query("SELECT * FROM ".$prefix."loairaovat where rvc_hienthi = 1 AND rvc_id_cha != 0 ORDER BY rvc_id DESC LIMIT 1");
		if($db->num_rows($rs) != 0){
			$row = $db->fetch_array($rs);
			$id = $row[rvc_id];
		}
		return $id;
	}
	
	function loadTopLoaiTuyenDung(){
		global $db, $prefix;
		$id = 0;
		$rs = $db->query("SELECT * FROM ".$prefix."loaituyendung where ltd_hienthi = 1 AND ltd_id_cha != 0 ORDER BY ltd_id DESC LIMIT 1");
		if($db->num_rows($rs) != 0){
			$row = $db->fetch_array($rs);
			$id = $row[ltd_id];
		}
		return $id;
	}
	
	function loadConfig($id){
		global $db,$prefix;
		$cf_value = "";
		$sql = "SELECT * FROM ".$prefix."cauhinh WHERE cf_id = '".$id."'";
		$rs = $db->query($sql);
		$row = $db->fetch_array($rs);
		$cf_value = $row['cf_value'];
		return $cf_value;
	}
	
	function updateConfig($id,$value){
		global $db,$prefix;
		$db->query("UPDATE ".$prefix."cauhinh SET cf_value = '".$value."' WHERE cf_id = '".$id."'");
	}
	
?>